<?php


class Request
{
    public $url;
    public $controller;
    public $action;
    public $params;
    private $_dispatched;
    
    public function Request () {
        $this->url = $_SERVER["REQUEST_URI"];
        $this->controller = NULL;
        $this->action = NULL;
        $this->isajax = false;
        $this->params = array();
    }
    
    public function setDispached ($flag) {
        $this->_dispatched = $flag;
        return $this;
    }
    public function isDispached () {
        return $this->_dispatched;
    }
    public function isPostBack () {
        return ($_SERVER["REQUEST_METHOD"]=="POST");
    }
    public function isAjax () {
        $val = $this->getVar("isajax", 'PARAM');
        return !empty($val);
    }

    public function getVar ($name, $method = 'POST', $default = '') {
        $method = strtoupper($method);
        $result = NULL;
        try {
            if ($method == 'PARAM') {
                $result = @array_key_exists($name, $this->params) ? $this->params[$name] : NULL;
            } else if ($method == 'POST') {
                $result = @array_key_exists($name, $_POST) ? $_POST[$name] : NULL;
            } else if ($method == 'GET') {
                $result = @array_key_exists($name, $_GET) ? $_GET[$name] : NULL;
            } else if ($method == 'SESSION') {
                $result = @array_key_exists($name, $_SESSION) ? $_SESSION[$name] : NULL;
            } else if ($method == 'COOKIE') {
                $result = @array_key_exists($name, $_COOKIE) ? $_COOKIE[$name] : NULL;
            } else {
                throw new JxException('invalid method prarm in getVar().');
            }
        } catch (Exception $ex) {
            $result = NULL;
        }
        
        if ($result === NULL) {
            return $default;
        } else {
            return $result;
        }
    }

    public function checkVar ($name, $method = 'POST') {
        $method = strtoupper($method);
        $result = False;
        if ($method == 'PARAM') {
            $result = @array_key_exists($name, $this->params);
        } else if ($method == 'POST') {
            $result = @array_key_exists($name, $_POST);
        } else if ($method == 'GET') {
            $result = @array_key_exists($name, $_GET);
        } else if ($method == 'SESSION') {
            $result = @array_key_exists($name, $_SESSION);
        } else if ($method == 'COOKIE') {
            $result = @array_key_exists($name, $_COOKIE);
        } else {
            throw new JxException('invalid method param in checkVar().');
        }

        return $result;
    }

    public function setCookie ($name, $var, $timeout = 3600) {
        setcookie($name, $var, time() + $timeout);
    }
    public function setSession ($name, $var) {
        $_SESSION[$name] = $var;
    }
    public function removeSession ($name) {
        session_unset($name);
    }
    public function getReferer () {
    	return $_SERVER["HTTP_REFERER"];
    }

}

